package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltStationAlarmBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltStationAlarmVo;
import com.ruoyi.zhgd.domain.NAsphaltStationAlarm;
import com.ruoyi.zhgd.mapper.NAsphaltStationAlarmMapper;
import com.ruoyi.zhgd.service.INAsphaltStationAlarmService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 沥青拌合站报警数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltStationAlarmServiceImpl implements INAsphaltStationAlarmService {

    private final NAsphaltStationAlarmMapper baseMapper;

    /**
     * 查询沥青拌合站报警数据
     */
    @Override
    public NAsphaltStationAlarmVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站报警数据列表
     */
    @Override
    public TableDataInfo<NAsphaltStationAlarmVo> queryPageList(NAsphaltStationAlarmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltStationAlarm> lqw = buildQueryWrapper(bo);
        Page<NAsphaltStationAlarmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站报警数据列表
     */
    @Override
    public List<NAsphaltStationAlarmVo> queryList(NAsphaltStationAlarmBo bo) {
        LambdaQueryWrapper<NAsphaltStationAlarm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltStationAlarm> buildQueryWrapper(NAsphaltStationAlarmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltStationAlarm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStationId()), NAsphaltStationAlarm::getStationId, bo.getStationId());
        lqw.eq(bo.getAsphaltAggregateRatioAlarm() != null, NAsphaltStationAlarm::getAsphaltAggregateRatioAlarm, bo.getAsphaltAggregateRatioAlarm());
        lqw.eq(bo.getGravel1Alarm() != null, NAsphaltStationAlarm::getGravel1Alarm, bo.getGravel1Alarm());
        lqw.eq(bo.getGravel2Alarm() != null, NAsphaltStationAlarm::getGravel2Alarm, bo.getGravel2Alarm());
        lqw.eq(bo.getGravel3Alarm() != null, NAsphaltStationAlarm::getGravel3Alarm, bo.getGravel3Alarm());
        lqw.eq(bo.getGravel4Alarm() != null, NAsphaltStationAlarm::getGravel4Alarm, bo.getGravel4Alarm());
        lqw.eq(bo.getGravel5Alarm() != null, NAsphaltStationAlarm::getGravel5Alarm, bo.getGravel5Alarm());
        lqw.eq(bo.getGravel6Alarm() != null, NAsphaltStationAlarm::getGravel6Alarm, bo.getGravel6Alarm());
        lqw.eq(bo.getCement1Alarm() != null, NAsphaltStationAlarm::getCement1Alarm, bo.getCement1Alarm());
        lqw.eq(bo.getRecycledMaterialAlarm() != null, NAsphaltStationAlarm::getRecycledMaterialAlarm, bo.getRecycledMaterialAlarm());
        lqw.eq(bo.getSlagPowderAlarm() != null, NAsphaltStationAlarm::getSlagPowderAlarm, bo.getSlagPowderAlarm());
        lqw.eq(bo.getSievingAlarm() != null, NAsphaltStationAlarm::getSievingAlarm, bo.getSievingAlarm());
        lqw.eq(bo.getTemperatureAlarm() != null, NAsphaltStationAlarm::getTemperatureAlarm, bo.getTemperatureAlarm());
        lqw.eq(bo.getAlarm() != null, NAsphaltStationAlarm::getAlarm, bo.getAlarm());
        lqw.eq(StringUtils.isNotBlank(bo.getDetails()), NAsphaltStationAlarm::getDetails, bo.getDetails());
        return lqw;
    }

    /**
     * 新增沥青拌合站报警数据
     */
    @Override
    public Boolean insertByBo(NAsphaltStationAlarmBo bo) {
        NAsphaltStationAlarm add = BeanUtil.toBean(bo, NAsphaltStationAlarm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站报警数据
     */
    @Override
    public Boolean updateByBo(NAsphaltStationAlarmBo bo) {
        NAsphaltStationAlarm update = BeanUtil.toBean(bo, NAsphaltStationAlarm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltStationAlarm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站报警数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
